<template>
  <div>
    <el-dialog
      :title="dialogInfo.isAdd?'添加分类':'编辑分类'"
      :visible.sync="dialogInfo.isShow"
      :before-close="cancel"
      center
    >
      <!-- :model="cateInfo" :rules="rules"  看作整体绑定 -->
      <el-form :model="cateInfo" :rules="rules" ref="cateInfo">


        <el-form-item label="上级分类" prop="pid" :label-width="formLabelWidth">
          <el-select v-model="cateInfo.pid" placeholder="请选择">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option
              v-for="item in cateInfo"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" prop="catename" :label-width="formLabelWidth">
          <el-input v-model="cateInfo.title" autocomplete="off"></el-input>
        </el-form-item>
        <!-- v-model="cateInfo.title"  prop  看作绑定到具体那个属性  -->
        <!-- el-form-item   用来写字 里面的prop 用来绑定验证    v-model在具体的输入框上面绑定双向-->
        
        <!-- el-form-item 为了让标签在一行 还要用它上面的prop去验证 label是Radio 的 value-->
       

     
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="cateInfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="dialogInfo.isAdd" @click="addCate">添加</el-button>
        <el-button type="primary" v-else @click="editMenu">编辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addCate, getMenuList, getcateInfo,postEditMenu} from "../../util/axios";
import { mapActions ,mapGetters} from "vuex";
export default {
  data() {
    return {
      cateInfo: {
      pid:'',
       catename:'',
       img:'',
       status:''
      },
      rules: {
        title: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在2到 8个字符", trigger: "blur" }
        ],
        pid: [{ required: true, message: "请选择上级分类", trigger: "change" }]
      },

      
      value: "",
      formLabelWidth: "120px", //label宽度
     
    };
  },
mounted(){
this.getMenuList()
},
  components: {},
  computed:{
    ...mapGetters({
      menuList:'menu/getMenuList'
    })
  },
  methods: {
    ...mapActions({
      getMenuList: "menu/getMenuListAction"
    }),
    cancel() {
      this.$emit("cancel", false);
      this.reset();
      //   console.log(this.cateInfo);
    },
    
    addCate() {
      this.$refs.cateInfo.validate(valid => {
        if (valid) {


          console.log(this.cateInfo);
          addCate(this.cateInfo).then(res => {
           
            if (res.data.code == 200) {
              this.$message.success(res.data.msg);
 console.log(res, "响应");
              this.cancel();
              // this.getMenuList();
            }else{
               this.$message.error(res.data.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
              return false;
        }
      });
    },
    editMenu() {
      this.$refs.cateInfo.validate(valid => {
        if (valid) {
         postEditMenu(this.cateInfo).then(res => {
            console.log(res, "响应");
            if (res.data.code == 200) {
              this.$message.success(res.data.msg);

              this.cancel();
              this.getMenuList();
            }else{
               this.$message.error(res.data.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
              return false;
        }
      });
    },
    reset() {
      (this.cateInfo = {
        pid: 0, //上级分类编号
        title: "", //标题
        icon: "", //图标
        type: 1, //类型1目录2分类
        url: "", //分类地址
        status: 1 //状态1正常2禁用
      }),
        this.$refs.cateInfo.resetFields(); //取消验证   ref="cateInfo"
    },         
    lookup(id){
            //  console.log(id,'menu通过ref方法传过来的');
          getcateInfo({id})
           .then(res=>{
            if(res.data.code==200){
              this.cateInfo=res.data.list  
              this.cateInfo.id=id                              //真实数据重新渲染   回显实现
           console.log(res);  //可以获取单条数据了
            }else{

              this.$message.error(res.data.msg)
            }
          
           
           })
    }
  },
  props: ["dialogInfo"]
};
</script>

<style lang="" scoped>
</style>
